//
//  ScrollWithImage.m
//  LunBoTuScroll
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 Zebra. All rights reserved.
//

#import "ScrollWithImage.h"
#import "UIImageView+WebCache.h"
@implementation ScrollWithImage


- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array withTitle:(NSArray *)titleArr
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatScrollView];
        _mutableArray = [[NSMutableArray alloc] initWithArray:array];
        _titleArray = [[NSMutableArray alloc] initWithArray:titleArr];
        [self addSubview:self.label];
        [self setArrayWithImage:array withTitleArray:titleArr];
        [self pageControl:array.count];
    }
    return self;
}

- (void)creatScrollView {
    _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scroll];
    _scroll.delegate = self;
    [_scroll release];
    _scroll.pagingEnabled = YES;
}

- (void)setArrayWithImage:(NSArray *)array withTitleArray:(NSArray *)titleArray {
    if (_mutableArray.count) {
        [_mutableArray insertObject:[_mutableArray lastObject] atIndex:0];
        [_mutableArray addObject:[_mutableArray objectAtIndex:1]];
    _imageNum = _mutableArray.count;
        [_titleArray insertObject:[_titleArray lastObject] atIndex:0];
        [_titleArray addObject:[_titleArray objectAtIndex:1]];
    
        
        
        
        
    for (int i = 0; i < _mutableArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height - 30)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, self.bounds.size.height - 30, self.bounds.size.width, 30)];
        label.backgroundColor = [UIColor whiteColor];
     
        
        
        label.text = _titleArray[i];
        
        
        [_scroll addSubview:label];
        [imageView sd_setImageWithURL:[NSURL URLWithString:_mutableArray[i]]];
        [_scroll addSubview:imageView];
        [imageView release];
        
        
       
        
        
        
        
        
    }
    _scroll.contentSize = CGSizeMake(_mutableArray.count * self.bounds.size.width, 0);
    _scroll.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _scroll.showsHorizontalScrollIndicator = NO;
    [self nstimerStart];
    };
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_scroll.contentOffset.x == 0) {
        _scroll.contentOffset = CGPointMake((_imageNum - 2) * self.bounds.size.width, 0);
    }
    else if (_scroll.contentOffset.x == (_imageNum - 1) * self.bounds.size.width) {
        _scroll.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    _pageControl.currentPage = _scroll.contentOffset.x / self.bounds.size.width - 1;
}

- (void)nstimerStart {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(startTurnImagePage) userInfo:nil repeats:YES];
}

- (void)startTurnImagePage {
    _scroll.contentOffset = CGPointMake(_scroll.contentOffset.x + self.bounds.size.width, 0);
    if (_scroll.contentOffset.x == 0) {
        _scroll.contentOffset = CGPointMake((_imageNum - 2) * self.bounds.size.width, 0);
    }
    else if (_scroll.contentOffset.x == (_imageNum - 1) * self.bounds.size.width) {
            _scroll.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    _pageControl.currentPage = _scroll.contentOffset.x / self.bounds.size.width - 1;
}

#warning 添加pageControl

- (void)pageControl:(NSInteger)pageNum {
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.8 * self.frame.size.width, self.frame.size.height - 40, 0, 0)];
    _pageControl.numberOfPages = pageNum;
    _pageControl.pageIndicatorTintColor = [UIColor redColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [_pageControl addTarget:self action:@selector(pageDidChange) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
    [_pageControl release];
}

- (void)pageDidChange {
    _scroll.contentOffset = CGPointMake((_pageControl.currentPage + 1) * self.bounds.size.width, 0);
}
@end
